package com.hibikiluler.javalearning.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hibikiluler.javalearning.entity.SysAdmins;
import com.hibikiluler.javalearning.mapper.SysAdminMapper;
import com.hibikiluler.javalearning.service.SysAdminService;
import com.hibikiluler.javalearning.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j

public class SysAdminServiceImpl implements SysAdminService {

    @Autowired
    private SysAdminMapper sysAdminMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Result insert(SysAdmins sysAdmins) {
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        sysAdminMapper.insert(sysAdmins);
        return Result.success("添加权限数据成功");
    }

    @Override
    public Result delete(Long id) {
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        sysAdminMapper.delete(id);
        return Result.success("删除权限数据成功");
    }

    @Override
    public Result update(SysAdmins sysAdmins) {
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        sysAdminMapper.update(sysAdmins);
        return Result.success("修改权限数据成功");
    }

    @Override
    public Result findPage(Queryinfo queryinfo) {
        log.info("开始权限数据分页-->页吗{},-->页数{}-->查询内容{},",queryinfo.getPagenumber(),queryinfo.getPagesize(),queryinfo.getQueryString());
        //第一个参数为页码 第二个为页数大小
        PageHelper.startPage(queryinfo.getPagenumber(), queryinfo.getPagesize());
        Page<SysAdmins> page = sysAdminMapper.findPage(queryinfo.getQueryString());
        long total = page.getTotal();
        List<SysAdmins> result = page.getResult();
        log.info("查询的总条数-->{}",total);
        log.info("分页列表-->{}",result);
        return PageResult.pageResult(total,result);
    }

    @Override
    public Result findAll() {
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("查询权限信息成功", sysAdminMapper.findAll());
    }
}
